
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include <time.h>
#include <SDL/SDL_image.h>

#include "cible.h"
#include "freeflycamera.h"
#include "scene.h"


GLuint GLLoadTexture(char *filename)
{
    SDL_Surface *surface;
    SDL_Surface *temp;
    GLuint texture;

    if ((temp = IMG_Load(filename)))
    {
        // Couleur transparente fixe (255, 0, 255)
        SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 0, 0, 0));
        surface = SDL_DisplayFormatAlpha(temp);

        if ((surface->w & (surface->w - 1)) != 0) {
            printf("Warning : %s's width is not a power of 2\n", filename);
        }

        // Also check if the height is a power of 2
        if ( (surface->h & (surface->h - 1)) != 0 ) {
            printf("Warning : %s's height is not a power of 2\n", filename);
        }

        // Have OpenGL generate a texture object handle for us
        glGenTextures(1, &texture);

        // Bind the texture object
        glBindTexture(GL_TEXTURE_2D, texture);

        // Set the texture's stretching properties
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

        // Edit the texture object's image data using the information SDL_Surface gives us
        glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0,
                      GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);

        SDL_FreeSurface (temp);
        SDL_FreeSurface (surface);
        return texture;
    }
    else {
        printf("SDL could not load image.bmp: %s\n", SDL_GetError());
        SDL_Quit();
    }

    return texture;
}




void Animation::Add(int x, int y, int w, int h, int delay)
{
    animation.push_back(Rect(x, y, w, h));
    this->delay.push_back(delay);
}

Rect Animation::Next()
{
    if (timer + delay.at(index) < SDL_GetTicks())
    {
        if (loop) {
            if (index == animation.size() - 1) looping = true;
            else if (index == 0) looping = false;
            if (looping)
                index--;
            else
                index++;
        }
        else
        {
            index++;
            if (index == animation.size())
                index = 0;
        }
        timer = SDL_GetTicks();
    }

    return animation.at(index);
}

void Animation::Loop(bool loop)
{
    this->loop = loop;
}


void Sprite::Draw()
{
    tile.clip = animation.Next();

    glBindTexture(GL_TEXTURE_2D, tile.texture);
    glColor3f(1.0f, 1.0f, 1.0f);

    glBegin(GL_QUADS);
        //(0, 0);
        glTexCoord2f((1.0f / tile.rect.w) * tile.clip.x, (1.0f / tile.rect.h) * tile.clip.y);
        glVertex2f(tile.rect.x, tile.rect.y);

        //(0, 1);
        glTexCoord2f((1.0f / tile.rect.w) * tile.clip.x, (1.0f / tile.rect.h) * (tile.clip.y + tile.clip.h));
        glVertex2f(tile.rect.x, tile.rect.y + tile.clip.h);

        //(1, 1);
        glTexCoord2f((1.0f / tile.rect.w) * (tile.clip.x + tile.clip.w), (1.0f / tile.rect.h) * (tile.clip.y + tile.clip.h));
        glVertex2f(tile.rect.x + tile.clip.w, tile.rect.y + tile.clip.h);

        //(1, 0);
        glTexCoord2f((1.0f / tile.rect.w) * (tile.clip.x + tile.clip.w), (1.0f / tile.rect.h) * tile.clip.y);
        glVertex2f(tile.rect.x + tile.clip.w, tile.rect.y);
    glEnd();
}

void Sprite::Draw(int x, int y)
{
    tile.rect.x = x;
    tile.rect.y = y;
    Draw();
}

void Sprite::LoadTexture(char *filename, int width, int height)
{
    tile.texture = GLLoadTexture(filename);
    tile.rect.w = width;
    tile.rect.h = height;
}

void Sprite::SetPosition(int x, int y)
{
    tile.rect.x = x;
    tile.rect.y = y;
}

void Sprite::AddAnimation(int x, int y, int w, int h, int delay)
{
    animation.Add(x, y, w, h, delay);
}


void animationSprite()
{

    srand(time(NULL));

    // Chargement des sprites et création des animations
    Sprite walk;
    walk.LoadTexture("Image/walk1.jpg", 1024, 1024);
    walk.AddAnimation(0, 0, 75, 123, 120);
    walk.AddAnimation(75, 0, 75, 123, 120);
    walk.AddAnimation(150, 0, 75, 123, 120);
    walk.AddAnimation(225, 0, 75, 123, 120);
    walk.AddAnimation(300, 0, 75, 123, 120);
    walk.AddAnimation(375, 0, 75, 123, 120);
    walk.AddAnimation(450, 0, 75, 123, 120);
    walk.AddAnimation(525, 0, 75, 123, 120);



    Sprite spritetab[6];
    spritetab[0] = walk;
    spritetab[1] = walk;
    spritetab[2] = walk;
    spritetab[3] = walk;
    spritetab[4] = walk;
    spritetab[5] = walk;

    Sprite showtab[spriteNb];
    for (int i = 0; i < spriteNb; i++)
    {
        showtab[i] = spritetab[rand() * 6 / RAND_MAX];
        showtab[i].SetPosition(rand() * (LARGEUR_FENETRE - 75) / RAND_MAX, rand() * (HAUTEUR_FENETRE - 123) / RAND_MAX);
    }


    for (int i = 0; i < spriteNb; i++)
    {
        showtab[i].Draw();
    }


}
